package com.klangner.go.app.strategy;

import java.util.Enumeration;

public class GameTree implements IGameTree {
  
    public GameTree(GoNodeInfo rootObject) {
            // create the first node, the root 
        this.root = new TreeNode(rootObject, null);
    } 

    public ITreeNode getRoot() {
        return root;
    }

    public ITreeNode getParent(ITreeNode node) {
        return node.getParent();
    }

    public boolean isLeaf(ITreeNode node) {
        return node.isLeaf();
    }

    public String toString() {
        String result = "";

        result += printTree( getRoot(), 0 );

        return result;
    }

    String printTree( ITreeNode node, int level ) {
        String result = "\n";

        for (int i = 0; i < level; ++i)
            result += " ";

        result += node.getContent().toString();

        for (Enumeration elements = node.getChildren().elements();
        	elements.hasMoreElements();) {
            ITreeNode child = (ITreeNode)elements.nextElement();
            result += printTree( child, level + 1 );
        }
        
        return result;
    }   
    
    protected ITreeNode root; 
}
